<?php
/**
 * 错误异常处理——Http接口
 */

namespace app\api\exception;
use think\exception\Handle;
use think\Response;
use Throwable;

class Http extends Handle{
    public $httpStatus = 500;
    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        if($e instanceof \think\Exception){
            $fun = $e->getTrace()[0]['class'].'\\'.$e->getTrace()[0]['function'];
            return returnResult($e->getCode(),config("config.describe.server_error"),'',500);
        }
        if($e instanceof \think\exception\HttpResponseException){
            return parent::render($request,$e);
        }
        //method_exists检查类的方法是否存在
        if(method_exists($e,"getStatusCode")){
            $httpStatus = $e->getStatusCode();
        }else{
            $httpStatus = $this->httpStatus;
        }
        errorLog('', $e->getMessage());
        return returnResult(500,config("config.describe.server_error"),[],$this->httpStatus);
    }
}
